package com.example.strategy.processor;

import com.example.strategy.enums.VipTypeEnum;
import com.example.strategy.service.VipProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author panghu
 * @version 1.0.0
 * @createTime 2023年07月26日 20:52:00
 * @Description
 */
@Service
@Slf4j
public class VipProcessorHolder {

    @Resource
    private Map<String, VipProcessor> vipProcessorMap;

    /**
     * 获得 vip处理器
     * @param vipTypeEnum
     * @return
     */
    public VipProcessor findVipProcessor(VipTypeEnum vipTypeEnum) {
        if (vipTypeEnum == null) {
            return null;
        }
        return findVipProcessor(vipTypeEnum.getClassPre());
    }

    /**
     * 获取vip的具体实现类
     * @param type
     * @return
     */
    private VipProcessor findVipProcessor(String type) {
        String className = generateClassName(type);
        VipProcessor vipProcessor = vipProcessorMap.get(className);
        if (vipProcessor == null) {
           throw new RuntimeException("传入的type有误!!!");
        }
        return vipProcessor;
    }

    /**
     * 拼接名称
     * @param type
     * @return
     */
    private String generateClassName(String type) {
        return type + VipProcessor.class.getSimpleName();
    }


}
